﻿package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;	
	import flash.events.Event;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.text.TextField;
	/*
	 * coded by moka 2009 / 06/ 22 
	 * name : moka's Interweave
	 */
	
	public class Main extends Sprite 
	{		
		private var bmp:Bitmap = addChild(new Bitmap(new BitmapData(640, 480, true, 0)))as Bitmap;
		private var bmpBuffer:BitmapData = bmp.bitmapData.clone();		
		private var shape:Shape = new Shape();
		
		private var followX:Array = new Array();		
		private var followY:Array = new Array();
		private var followSegment:uint = 30;
		private var followDelta:uint = 3;

		public function Main():void 
		{		
			shape = new Shape();			
			for (var i:uint = 0; i < followSegment; i++ )(followX.push(0), followY.push(0));				
			
			stage.scaleMode = "noScale"; 
			stage.addEventListener(Event.ENTER_FRAME, onProcess);
			(addChild(new TextField()) as TextField).htmlText = "<font color='#FFFFFF'>Moka's work 2009</font>"; 
		}		
		
		private function onProcess(e:Event):void {
			
			shape.graphics.clear();
			shape.graphics.lineStyle(1, 0xFFCC00, 1);
			shape.graphics.moveTo(mouseX, mouseY);
			
			for (var i:uint = 0; i < followSegment; i++ )
			{
				if (i == 0){
					followX[0] += (mouseX - followX[0]) / followDelta;
					followY[0] += (mouseY - followY[0]) / followDelta;
					shape.graphics.lineTo(followX[0], followY[0]);
				}else {
					followX[i] += (followX[i-1] - followX[i]) / followDelta;
					followY[i] += (followY[i-1] - followY[i]) / followDelta;
					shape.graphics.lineTo(followX[i], followY[i]);
				}	
			}
						
			// 背景繪製
			bmpBuffer.fillRect(bmpBuffer.rect, 0xCC000000);
			bmpBuffer.draw(shape);
			bmp.bitmapData.draw(bmpBuffer,new Matrix,new ColorTransform(0.5,0.5,0.5,0.5),"screen");
		}   
	}	
}